package com.example.teachingSystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teachingSystem.mapper.*;
import com.example.teachingSystem.pojo.entity.*;
import com.example.teachingSystem.pojo.form.course.AddCourseForm;
import com.example.teachingSystem.pojo.form.course.CoursePageQuery;
import com.example.teachingSystem.pojo.form.course.UpdateCourseForm;
import com.example.teachingSystem.pojo.vo.OptionVo;
import com.example.teachingSystem.pojo.vo.course.CourseVo;
import com.example.teachingSystem.service.CourseService;
import com.example.teachingSystem.utils.JwtHelper;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import com.example.teachingSystem.utils.result.ResultCodeEnum;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 86147
 * @description 针对表【course(课程表)】的数据库操作Service实现
 * @createDate 2023-12-10 23:57:59
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
        implements CourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private EnrollmentMapper enrollmentMapper;

    @Autowired
    private JwtHelper jwtHelper;
    @Autowired
    private HttpServletRequest request;

    @Override
    public Result<String> addCourse(AddCourseForm addCourseForm) {
        Course one = new Course();
        BeanUtils.copyProperties(addCourseForm, one);
        this.save(one);
        return Result.ok(null, "添加成功");
    }


    @Override
    public Result<String> updateCourse(UpdateCourseForm updateCourseForm) {
        Course one = courseMapper.selectById(updateCourseForm.getCourseId());
        if (one == null) {
            return Result.build(null, 500, "不存在该课程");
        }
        BeanUtils.copyProperties(updateCourseForm, one);
        this.updateById(one);
        return Result.ok(null, "修改成功");
    }

    @Override
    public Result<String> deleteCourse(Long id) {
        Course one = courseMapper.selectById(id);
        if (one == null) {
            return Result.build(null, 500, "不存在该课程");
        }
        this.removeById(one);
        new Thread(() -> enrollmentMapper.delete(new LambdaQueryWrapper<Enrollment>()
                .eq(Enrollment::getCourseId, id))).start();
        return Result.ok(null, "删除成功");
    }

    @Override
    public Result<List<CourseVo>> getOptionalCoursesByStu(Long semesterId) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return Result.build(null, ResultCodeEnum.USER_ERROR);
        }
        List<CourseVo> courseVos = courseMapper.getOptionalCoursesByStu(semesterId, Long.parseLong(user.getUsername()));
        return Result.ok(courseVos);
    }

    @Override
    public PageResult<CourseVo> getMyCoursePageByStu(CoursePageQuery coursePageQuery) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return PageResult.build(null, ResultCodeEnum.USER_ERROR);
        }
        Page<Course> page = new Page<>(coursePageQuery.getPageNum(), coursePageQuery.getPageSize());
        Student student = studentMapper.selectOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getStudentId, Long.parseLong(user.getUsername())));
        if (student != null) {
            IPage<CourseVo> courseVoIPage = courseMapper.getMyCoursePageByStu(page, coursePageQuery, student.getStudentId());
            return PageResult.success(courseVoIPage);
        }
        return PageResult.build(null, 500, "当前用户无权限");
    }

    @Override
    public PageResult<CourseVo> getMyCoursePageByTea(CoursePageQuery coursePageQuery) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return PageResult.build(null, ResultCodeEnum.USER_ERROR);
        }
        Page<Course> page = new Page<>(coursePageQuery.getPageNum(), coursePageQuery.getPageSize());
        Teacher teacher = teacherMapper.selectOne(new LambdaQueryWrapper<Teacher>()
                .eq(Teacher::getTeacherId, Long.parseLong(user.getUsername())));
        if (teacher != null) {
            IPage<CourseVo> courseVoIPage = courseMapper.getMyCoursePageByTea(page, coursePageQuery, teacher.getTeacherId());
            return PageResult.success(courseVoIPage);
        }
        return PageResult.build(null, 500, "当前用户无权限");
    }

    @Override
    public PageResult<CourseVo> getAllCoursePage(CoursePageQuery coursePageQuery) {
        // 获取请求头中的令牌
        String token = request.getHeader("Authorization");
        Long userId = jwtHelper.getUserId(token);
        SysUser user = sysUserMapper.selectById(userId);
        if (user == null) {
            return PageResult.build(null, ResultCodeEnum.USER_ERROR);
        }
        Page<Course> page = new Page<>(coursePageQuery.getPageNum(), coursePageQuery.getPageSize());
        Admin admin = adminMapper.selectOne(new LambdaQueryWrapper<Admin>()
                .eq(Admin::getAdminId, Long.parseLong(user.getUsername())));
        if (admin != null) {
            IPage<CourseVo> courseVoIPage = courseMapper.getAllCoursePage(page, coursePageQuery);
            return PageResult.success(courseVoIPage);
        }
        return PageResult.build(null, 500, "当前用户无权限");
    }

    @Override
    public Result<List<OptionVo>> getTeacherOptions() {
        return Result.ok(courseMapper.getTeacherOptions());
    }

}




